package com.lagou.studentsystem.servlet;

import com.lagou.studentsystem.dao.ClassesDao;
import com.lagou.studentsystem.dao.Impl.ClassesDaoImpl;
import com.lagou.studentsystem.model.Classes;
import com.lagou.studentsystem.model.Student;
import com.lagou.studentsystem.service.StudentService;
import com.lagou.studentsystem.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "FindClassesServlet", urlPatterns = "/findClassesByidOrName")
public class FindClassesByIdOrNameServlet extends HttpServlet {

    private ClassesDao classesDao = new ClassesDaoImpl();
    private List<Classes>classesList;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
            String c_id = request.getParameter("id");
            String c_teacher = request.getParameter("name");
            if (null != c_id && !(c_id.equals("")) ) {
                Classes classesById = classesDao.getClassesById(c_id);
                if (null != classesById) {
                    classesList = new ArrayList<>();
                    classesList.add(classesById);
                    request.getSession().setAttribute("classesList", classesList);
                    response.sendRedirect("class.jsp");
                }
            } else if (null != c_teacher ) {
                classesList = classesDao.getClassesByName(c_teacher);
                if (null != classesList) {
                    request.getSession().setAttribute("classesList", classesList);
                    response.sendRedirect("class.jsp");
                }
            }

        }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
